﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Csharp2Python3
{
    public partial class FormSql : Form
    {
        public FormSql()
        {
            InitializeComponent();
        }

        private void FormSql_Load(object sender, EventArgs e)
        {
            var dbconfig = FileHelper.ReadFile("config.json");
            //Console.WriteLine(dbconfig);
            JObject obj = JObject.Parse(dbconfig);
            TableLayoutPanel layoiutpan = tableLayoutPanel1;
            //layoiutpan.set
            layoiutpan.ColumnCount = 2;
            layoiutpan.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            layoiutpan.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            // layoiutpan.Location = new System.Drawing.Point(173, 96);
            //layoiutpan.Name = "tableLayoutPanel1";
            layoiutpan.RowCount = obj.Children().Count();
            //layoiutpan.Size = new System.Drawing.Size(200, 300);
            layoiutpan.AutoSize = true;
            layoiutpan.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            layoiutpan.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            layoiutpan.TabIndex = 2;

            int column = 0; int row = 0;
            foreach (JProperty item in obj.Children())
            {
                //obj.Name
                //  Console.WriteLine(item.Name);
                Label lable = new Label();
                lable.Text = item.Name;
                lable.TextAlign = ContentAlignment.MiddleCenter;
                layoiutpan.Controls.Add(lable, 0, column);
                TextBox box = new TextBox();
                box.Text = item.Value.ToString();
                box.TextAlign = HorizontalAlignment.Left;
                box.Name = item.Name;
                //box
                // box.ReadOnly = true;
                layoiutpan.Controls.Add(box, 1, column);
                column++;
            }
            Button btn1 = new Button();
            btn1.Text = "保存";
            layoiutpan.Controls.Add(btn1, 1, column);
            btn1.MouseClick  += new MouseEventHandler(button_MouseClick);


            
        }
        private void button_MouseClick(object sender, MouseEventArgs e) {
            object ob = sender;

            Button button = (Button)sender;
            TableLayoutControlCollection collection = tableLayoutPanel1.Controls;
            //collection.
            JObject rootObj = new JObject();
            foreach (Control c  in tableLayoutPanel1.Controls)
            {
                //.// t
                    if(c is TextBox)
                {
                    TextBox box = (TextBox) c;
                    Console.WriteLine(box.Name+"-->"+box.Text);
                    JObject obj = new JObject();
                    rootObj.Add(box.Name , box.Text);
                    //obj.put
                    //rootObj.Add();
                }
               
            }
           

            Console.WriteLine(JsonConvert.SerializeObject(rootObj));
            FileHelper.WriteFile("config.json", JsonConvert.SerializeObject(rootObj));
            MessageBox.Show("保存成功");


        }
    }
}
